package com.deviche.gank;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadMoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import java.io.IOException;
import java.util.ArrayList;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {
    private RecyclerView recyclerView;
    private ArrayList<ImageBean.ResultsBean> mList;
    private ImageAdapter adapter;
    private RefreshLayout refreshLayout;
    public static final String TAG = MainActivity.class.getSimpleName();
    public static final int UPDATE = 2000;
    public static int START = 1;
    public static final String URL = "imageUrl";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            //super.handleMessage(msg);
            if (msg.what == UPDATE) {
                //adapter.notifyDataSetChanged();

                //设置Adapter
                adapter = new ImageAdapter(R.layout.item, mList);
                adapter.openLoadAnimation(BaseQuickAdapter.SLIDEIN_BOTTOM);
                Log.e(TAG, "handleMessage: " + mList.size());
                adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
                    @Override
                    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                        ImageBean.ResultsBean resultsBean = (ImageBean.ResultsBean) adapter.getItem(position);
                        String imageUrl = resultsBean.getUrl();
                        Intent intent = new Intent(MainActivity.this,ImageDetailActivity.class);
                        intent.putExtra(URL,imageUrl);
                        startActivity(intent);
                    }
                });
                recyclerView.setAdapter(adapter);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
        refreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                refreshlayout.finishRefresh(2000/*,false*/);//传入false表示刷新失败
                mList.clear();
                START = 0;
                getImages();
            }
        });
        refreshLayout.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(RefreshLayout refreshlayout) {
                refreshlayout.finishLoadMore(2000/*,false*/);//传入false表示加载失败
                getImages();
            }
        });

        recyclerView = findViewById(R.id.rv);
        //LinearLayoutManager layoutmanager = new LinearLayoutManager(this);
        GridLayoutManager layoutmanager = new GridLayoutManager(this, 2);
        //设置RecyclerView 布局
        recyclerView.setLayoutManager(layoutmanager);
        getImages();
    }

    private void getImages() {
        mList = new ArrayList<>();

        OkHttpClient client = new OkHttpClient();//创建OkHttpClient对象
        Request request = new Request.Builder()
                //.url(Constant.URL+"1")//请求接口。如果需要传参拼接到接口后面。
                .url("http://gank.io/api/data/福利/10/" + START)
                .build();//创建Request 对象
        client.newCall(request).enqueue(new Callback() {
                    @Override
                    public void onFailure(Call call, IOException e) {
                        Log.e(TAG, "onFailure: " + "");
                    }

                    @Override
                    public void onResponse(Call call, Response response) throws IOException {
                        START++;
                        String data = response.body().string();
                        Log.e(TAG, "onResponse: " + data);
                        Gson gson = new Gson();
                        ImageBean imageBean = gson.fromJson(data, ImageBean.class);
                        mList = imageBean.getResults();
                        Log.e(TAG, "onResponse: " + mList.size());
                        Message msg = Message.obtain();
                        msg.what = UPDATE;
                        handler.sendMessage(msg);
                    }
                }
        );
        /*//设置Adapter
        adapter = new ImageAdapter(R.layout.item, mList);
        Log.e(TAG, "handleMessage: " + mList.size());
        recyclerView.setAdapter(adapter);*/
    }
}
